package com.project.config;

import com.project.shiro.CustomRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    @Bean
    public DefaultWebSecurityManager getManager(CustomRealm realm){
        DefaultWebSecurityManager manager=new DefaultWebSecurityManager();
        manager.setRealm(realm);
        return manager;
    }
    @Bean("shiroFilterFactoryBean")
    public ShiroFilterFactoryBean getBean(DefaultWebSecurityManager manager){
        ShiroFilterFactoryBean bean=new ShiroFilterFactoryBean();
        bean.setSecurityManager(manager);
        //未登录时，调用受限资源
        bean.setLoginUrl("/user/loginError");
        Map<String ,String > map=new LinkedHashMap<>();
        map.put("/user/login","anon");
        map.put("/swagger-ui/**", "anon");
        map.put("/swagger/**", "anon");
        map.put("/swagger-resources/**","anon");
        map.put("/v2/**","anon");

        map.put("/**","authc");
        bean.setFilterChainDefinitionMap(map);
        return bean;
    }
}
